﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Newtonsoft.Json;
namespace Ccit.OA.Email
{
    public partial class email_wbyj_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["XH"] = (null != Request.QueryString["XH"]) ? Request.QueryString["XH"].ToString() : "";
                InitPage();
            }
        }

        public void InitPage()
        {
            string strXH = ViewState["XH"].ToString().Trim() ;

            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;

            hidYGJB.Value = loginUser.ClassId;
            hidYGBM.Value = loginUser.DeptId;
            hidYGZW.Value = loginUser.PosId;
            hidJG.Value = loginUser.OrganId;
            hidTime.Value = DateTime.Now.ToString().Trim();
            hidUserId.Value = loginUser.UserId;
            hidName.Value = loginUser.UserName;

            EmailBLL bll = new EmailBLL();

            #region
            MsEmailPersonal msPerson = new MsEmailPersonal();
            msPerson.userId = loginUser.UserId;
            List<MsEmailPersonal> msPersonList = bll.GetFolderList(msPerson);
            ddFolder.Items.Clear();
            ddFolder.Items.Add(new ListItem("系统默认收件箱", "0"));
            if (null != msPersonList && msPersonList.Count >= 1)
            {
                foreach (MsEmailPersonal ms in msPersonList)
                {
                    ddFolder.Items.Add(new ListItem(ms.foldertitle, ms.folderId));
                }
            }
            #endregion 

            if (strXH != "")
            {
                MsEmailAccount msAccount = new MsEmailAccount();
                msAccount.Id = strXH;
                msAccount = bll.GetAccount(msAccount);

                hidId.Value = msAccount.Id;
                txtAccountName.Text = msAccount.accountName;
                txtAddress.Text = msAccount.address;
                txtSMTP.Text = msAccount.receiveSMTP;
                txtSMTPPort.Text = msAccount.smtpPort;
                if (msAccount.sendemlyzLogo == "0")
                {
                    cbMR.Checked = false;
                }
                else {
                    //需要验证SMTP服务器 身份时候
                    cbMR.Checked = true;
                    tdSFYZ.Style.Add(HtmlTextWriterStyle.Display, "block");

                    //使用与POP3服务器相同的信息
                    if (msAccount.pop3LogName == msAccount.smtpLogName && msAccount.pop3Pass == msAccount.smtpPass)
                    {
                        rbPOP.Checked = true;
                    }
                    //使用SMTP新身份信息
                    else {
                        rbYZXX.Checked = true;
                        txtZH.Text = msAccount.smtpLogName;
                        txtPass.Text = msAccount.smtpPass;
                        txtPass.Attributes.Add("value", msAccount.pop3Pass);
                    }
                }
                txtpop3.Text = msAccount.receivePOP3;
                txtpopPort.Text = msAccount.pop3Port;
                txtZHPOP3.Text = msAccount.pop3LogName;
                txtpassPOP3.Text = msAccount.pop3Pass;
                txtpassPOP3.Attributes.Add("value", msAccount.pop3Pass);
                if(msAccount.delWebLogo == "1")
                {
                    rbysDel.Checked = true;
                    rbnoDel.Checked = false;
                }
                else{
                    rbnoDel.Checked = true;
                    rbysDel.Checked = false;
                }

                ddFolder.SelectedIndex = int.Parse(msAccount.receiveFolder);
                txtOrder.Text = msAccount.order;

                if (msAccount.changesLogo == "1")
                {
                    rbysDef.Checked = true;
                    rbnoDef.Checked = false;
                }
                else
                {
                    rbysDef.Checked = false;
                    rbnoDef.Checked = true;
                }

                msAccount = null;
            }

            txtpopPort.Attributes.Add("onkeyup", "CliNumber('txtpopPort');");
            txtSMTPPort.Attributes.Add("onkeyup", "CliNumber('txtSMTPPort');");

            bll.Release();
            bll = null;
            msPersonList = null;
            msPerson = null;
        }

        /// <summary>
        /// ajax方法 保存外部邮件箱信息
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string Save(string serExpr)
        {
            bool bFlag = false;
            string operType = string.Empty;
            MsEmailAccount msAccount = (MsEmailAccount)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailAccount));
            EmailBLL bll = new EmailBLL();
            if (msAccount.Id == "")
            {
                msAccount = bll.AddAccount(msAccount);
                if (msAccount.Id != "")
                {
                    bFlag = true;
                }
                operType = "insert";
            }
            else
            {
                bFlag = bll.UpdateAccount(msAccount);
                operType = "update";
            }
            string sResult = (bFlag) ? "1" : "0";
            var newObj = new { operType = operType, resultSign = sResult ,resultId = msAccount.Id,resultName = msAccount.accountName};
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msAccount = null;
            return resultSerExpr;
        }
    }
}
